package com.unina.dropplayer.utilities;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.unina.dropplayer.common.DropPlayerConstants;

public class DropPlayerSessionBuilder {
	
	 public static AndroidAuthSession buildSession(Context context) {
	        AppKeyPair appKeyPair = new AppKeyPair(DropPlayerConstants.APP_KEY, DropPlayerConstants.APP_SECRET);
	        AndroidAuthSession session;

	        String[] stored = getKeys(context);
	        if (stored != null) {
	            AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
	            session = new AndroidAuthSession(appKeyPair, DropPlayerConstants.ACCESS_TYPE, accessToken);
	        } else {
	            session = new AndroidAuthSession(appKeyPair, DropPlayerConstants.ACCESS_TYPE);
	        }

	        return session;
	    }
	    
	 public static void clear(Context context) {
	        SharedPreferences prefs = context.getSharedPreferences(DropPlayerConstants.PREFS_NAME, 0);
	        Editor editor = prefs.edit();
	        editor.clear();
	        editor.commit();
	    }
	 
	 public static String[] getKeys(Context context) {
	        SharedPreferences prefs = context.getSharedPreferences(DropPlayerConstants.PREFS_NAME, 0);
	        String key = prefs.getString(DropPlayerConstants.PREF_APP_KEY_TOKEN, null);
	        String secret = prefs.getString(DropPlayerConstants.PREF_APP_SECRET_TOKEN, null);
	        if (key != null && secret != null) {
	        	String[] ret = new String[2];
	        	ret[0] = key;
	        	ret[1] = secret;
	        	return ret;
	        } else {
	        	return null;
	        }
	    }
	 

}
